﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinHID.USB;

namespace WinHID
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnReadDT_Click(object sender, EventArgs e)
        {
            try
            {
                HIDHelper.Open();
                if (HIDHelper.isOpen)
                {
                    PrintOriginal($"{DateTime.Now.ToLongTimeString()} USB打开\r\n");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{DateTime.Now.ToLongTimeString()} 打开异常:{ex}\r\n");
                return;
            }

            try
            {
                SCD send = new SCD("FA", "03", "");
                PrintOriginal($"{DateTime.Now.ToLongTimeString()} Send03:{send.HEX}\r\n");
                SCD read = HIDHelper.Collect(send, 10);
                PrintOriginal($"{DateTime.Now.ToLongTimeString()} Read03:{read.HEX}\r\n");

                DateTime dt = new DateTime(2000 + read.BYTES[6], read.BYTES[7], read.BYTES[8], read.BYTES[9], read.BYTES[10], read.BYTES[11]);
                PrintOriginal($"{DateTime.Now.ToLongTimeString()} 读设备时间,成功:{dt}\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{DateTime.Now.ToLongTimeString()} 通讯异常:{ex}\r\n");
                return;
            }
            finally
            {
                if (HIDHelper.isOpen)
                {
                    HIDHelper.Close();
                }
                PrintOriginal($"{DateTime.Now.ToLongTimeString()} USB关闭\r\n");
            }

        }
        private void PrintOriginal(string msg)
        {
            this.Invoke(new Action(() =>
            {
                txtLog.AppendText(msg);
            }));
        }
    }
}
